

class Solution:
    def isSymmetric(self, root: TreeNode) -> bool:
        def recur(L, R):
            if not L and not R: return True
            if not L or not R or L.val != R.val: return False
            return recur(L.left, R.right) and recur(L.right, R.left)

        return recur(root.left, root.right) if root else True
# 判断B是否是A的子结构
class Solution2:
    def isSubStructure(self, A: TreeNode, B: TreeNode) -> bool:
        def recur(A, B):
            if not B: return True
            if not A or A.val != B.val: return False
            return recur(A.left, B.left) and recur(A.right, B.right)

        return bool(A and B) and (recur(A, B) or self.isSubStructure(A.left, B) or self.isSubStructure(A.right, B))
# 同上
class Solution3:
    def isSubStructure(self, A: TreeNode, B: TreeNode) -> bool:
        if not (A and B):
            return False
        else:
            r1 = self.recur(A, B)
            r2 = self.isSubStructure(A.left, B)
            r3 = self.isSubStructure(A.right, B)
            return (r1 | r2 | r3)

    def recur(self, A, B):
        if not B:
            return True
        if not A or A.val != B.val:
            return False
        return self.recur(A.left, B.left) and self.recur(A.right, B.right)
